function [best_param, error_vect, std_vect] = cv_wrapper_dt(X, Y, opt_param)

% This function takes in a column vector OPT_PARAM (optimization parameters) and
% calls the approriate cv function in a loop for each value in OPT_PARAM to
% return the best choice of parameter, along with a matrix of errors and a matrix
% of std devs, both are (length of param)x2. the error columns are [cv_err test_err],
% and the std cols are [cv_std test_std].

n = size(X, 1);
n_folds = 10;

%choose the number of partitions
part = make_cv_partition(n, n_folds);

cv_ind = find(part~= 9 & part~= 10);
test_ind = find(part==9 | part == 10);

%Index the X and Y arrays by the appropriate indicies

cv_points = X(cv_ind,:); %Training set (80%)
cv_labels = Y(cv_ind,:); %Training labels (80 % )
test_points = X(test_ind,:); %Test set (20 %)
test_labels = Y(test_ind,:); % Test labels (20 % )

error_vect = zeros(size(opt_param,1),2);
std_vect = zeros(size(opt_param,1),2);

for i=1:size(opt_param,1)
    [error_vect(i,:) std_vect(i,:)] = cv_dt(opt_param(i), ...
    cv_points, cv_labels, test_points, test_labels);
end

% Find the index of the minimum error, and index the OPT_PARAM vector with
% that index to find the corresponding BEST_PARAM
[~,ind] = min(error_vect(:,1));
best_param = opt_param(ind);

%Plot errors
figure(1); clf; hold on; grid on;
errorbar(opt_param, error_vect(:,1), std_vect(:,1), '-r', 'LineWidth', 2);
errorbar(opt_param, error_vect(:,2), std_vect(:,2),'-b', 'LineWidth', 2);
legend('CV Error', 'Test Error');

title('CV and True Error for Decision Trees','FontSize',20)
xlabel('Parameter Value','FontSize',18)
ylabel('Error','FontSize',18)
hold off;

end